﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Domain.Entities;

namespace wt.wine.Application.ManageDevicetable.Command
{
    /// <summary>
    /// 添加设备
    /// </summary>
    public class AddDeviceCommand:IRequest<Json<string>>
    {
        /// <summary>
        /// 设备编码
        /// </summary>
        public string deviceid { get; set; }
        /// <summary>
        /// 自定义设备编码
        /// </summary>
        public string cusdeviceid { get; set; }
        /// <summary>
        /// 产品id
        /// </summary>
        public int skuid { get; set; }
        /// <summary>
        /// 设备最大容量
        /// </summary>
        public int maxcapacity { get; set; }
        /// <summary>
        /// 设备容量阈值
        /// </summary>
        public int capacitythreshold { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string remark { get; set; }
    }

    public class AddDeviceCommandHandler : IRequestHandler<AddDeviceCommand, Json<string>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IDevicetableRepository _idevicetableRepository;
        public AddDeviceCommandHandler(ICurrentUserService icurrentUserServices, IDevicetableRepository idevicetableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _idevicetableRepository = idevicetableRepository;
        }
        public async Task<Json<string>> Handle(AddDeviceCommand request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserServices.GetUserId();
            if (userid <= 0)
            {
                return new Json<string>() { Status = 1003, Detail = "请先登录" };
            }
            if (string.IsNullOrWhiteSpace(request.deviceid))
            {
                return new Json<string>() { Status = 1003, Detail = "设备ID编码不能为空" };
            }
            else if ((await _idevicetableRepository.GetDeviceCount(0, request.deviceid,cancellationToken)) > 1)
            {
                return new Json<string>() { Status = 1003, Detail = "设备ID编码已经录入系统" };
            }
            devicetable model = new devicetable();
            model.deviceid = request.deviceid;
            model.cusdeviceid = request.cusdeviceid;
            model.skuid = request.skuid;
            model.maxcapacity = request.maxcapacity;
            model.capacitythreshold = request.capacitythreshold;
            model.currentcapacity = 0;
            model.userid = userid;
            model.userpid = userid;
            //model.binduserid = 0;
            model.state = 0;
            model.createtime = DateTime.Now;
            model.updatetime = DateTime.Now;
            model.remark = request.remark;
            await _idevicetableRepository.AddDeviceTable(model, cancellationToken);

            return new Json<string>() { Status=1000, Detail="添加成功" };
        }
    }
}
